<?php
namespace App\Http\Controllers\Admin;
use App\Models\ArticleType;
use App\Services\CommonService;
use Illuminate\Http\Request;

//信息配置
class InfoConfigController extends BaseController
{

    //信息配置列表
    public function infoConfigList()
    {
        $data = ArticleType::GetParentCategory(array('parentid' => 0,'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
        if (empty($data)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章一级分类信息为空', array());
        }
        foreach ($data as $k => $v) {
            $son_type = ArticleType::GetSecondCategory(array('parentid' => $v['id'],'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
            $data[$k]['children'] = $son_type;
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //添加或编辑信息配置
    public function addOrEditInfoConfig(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) { //添加
            if ($input['parentid'] === '' || $input['parentid'] === null) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择一级分类id');
            }
            if (empty($input['type_name'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入分类名称');
            }
            if ($input['sort'] === '' || $input['sort'] === null) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入排序');
            }
            if (empty($input['status'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择状态');
            }
            $res = ArticleType::addData($input);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
            }
        } else { //编辑
            if(isset($input['status']) && $input['status'] == ArticleType::ARTICLE_TYPE_STATUS_HIDE){ //状态 1显示  2隐藏
                $second = ArticleType::GetSecondCategory(array('parentid' => $input['id'],'status' => ArticleType::ARTICLE_TYPE_STATUS_DISPLAY));
                if(!empty($second)){
                    //修改一级分类状态为隐藏时直接也隐藏二级分类状态
                    foreach ($second as $k => $v) {
                        ArticleType::editDataById($v['id'], array('status' => $input['status']));
                    }
                }
            }
            ArticleType::editDataById($input['id'], $input);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
        }

    }

    //根据id查询信息配置
    public function infoConfigById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章分类id为空', array());
        }
        $data = ArticleType::GetCategoryById($input['id']);
        if (empty($data)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章分类数据为空', array());
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //根据id编辑文章分类状态
    public function editArticleTypeStatus(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章分类id为空', array());
        }
        if (empty($input['status'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '状态为空', array());
        }
        if($input['status'] == ArticleType::ARTICLE_TYPE_STATUS_HIDE){ //状态 1显示  2隐藏
            $second = ArticleType::GetSecondCategory(array('parentid' => $input['id'],'status' => ArticleType::ARTICLE_TYPE_STATUS_DISPLAY));
            if(!empty($second)){
                //修改一级分类状态为隐藏时直接也隐藏二级分类状态
                foreach ($second as $k => $v) {
                    ArticleType::editDataById($v['id'], array('status' => $input['status']));
                }
            }
        }
        ArticleType::editDataById($input['id'], array('status' => $input['status']));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
    }

    //根据id删除信息配置
    public function delInfoConfig(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章分类id为空');
        }
        $second = ArticleType::GetSecondCategory(array('parentid' => $input['id'],'is_delete' => ArticleType::ARTICLE_TYPE_NOT_DELETE));
        if(!empty($second)){
            //删除一级分类时直接也删除二级分类
            foreach ($second as $k => $v) {
                ArticleType::editDataById($v['id'], array('is_delete' => ArticleType::ARTICLE_TYPE_IS_DELETE));
            }
        }
        ArticleType::editDataById($input['id'], array('is_delete' => ArticleType::ARTICLE_TYPE_IS_DELETE)); //是否删除  1是 2否
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
    }

}
